package com.zeny.coding.dto;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.enmus.CourseChargeEnum;
import com.zeny.coding.util.DtoUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.util.StringUtils;

import javax.xml.bind.ValidationException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@SuppressWarnings("all")
public class CourseDto {
    private String id;

    private String name;

    private String summary;

    private Integer time;

    private BigDecimal price;

    private String image;

    private String level;

    private String charge;

    private String status;

    private Integer enroll;

    private Integer sort;

    private Date createdAt;

    private Date updatedAt;

    private String teacherId;

    private List<String> categorys;


    public void isValidForUpdate() throws ValidationException {
        DtoUtil.valid(this.id, true, 1, 19);
        valid();
    }

    public void isValidForSave() throws ValidationException {
        valid();
        if (!StringUtils.isEmpty(this.id)) {
            this.id = null;
        }

    }

    private void valid() throws ValidationException {
        //检查课程名称
        DtoUtil.valid(this.name, true, 1, 50);
        //检查课程概述
        DtoUtil.valid(this.summary, false, 1, 2000);
        //检查课程价格
        if (this.price == null || this.price.compareTo(BigDecimal.ZERO) == -1) {
            throw new ValidationException(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        //检查课程封面
        DtoUtil.valid(this.image, false, 1, 100);
        //检查课程级别
        DtoUtil.valid(this.level, true, 1, 1);
        //检查课程收费状态
        DtoUtil.valid(this.charge, true, 1, 1);
        //检查课程状态
        DtoUtil.valid(this.status, true, 1, 1);
        //检查顺序
        DtoUtil.require(this.sort);
        //检查讲师
        DtoUtil.valid(this.teacherId, true, 1, 19);

        //如果是免费状态, 价格设置为 0
        if (this.charge.equals(CourseChargeEnum.FREE.getKey())) {
            this.price = BigDecimal.ZERO;
        }
        //如果价格为0,则是免费状态
        if (this.price.compareTo(BigDecimal.ZERO) == 0) {
            this.charge = CourseChargeEnum.FREE.getKey();
        }


    }
}